package com.daxt.common.config;

import java.util.List;

import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.collect.Lists;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@ConditionalOnExpression("${swagger.onlion}")
public class SwaggerConfig {

	//@Value("${spring.profiles.active}")
	private String active = "dev";

	@Bean(name = "Api1")
	public Docket api() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).useDefaultResponseMessages(false)
				.groupName("公共").enable(true).select().apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.regex("^(?!auth).*$"))
				.apis(RequestHandlerSelectors.basePackage("com.daxt.controller.index")).paths(PathSelectors.any())
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
				.build().securitySchemes(securitySchemes()).securityContexts(securityContexts());

	}

	@Bean(name = "Api2")
	public Docket api2() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).useDefaultResponseMessages(false)
				.groupName("系统").enable(true).select().apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.regex("^(?!auth).*$"))
				.apis(RequestHandlerSelectors.basePackage("com.daxt.controller.system")).paths(PathSelectors.any())
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
				.build().securitySchemes(securitySchemes()).securityContexts(securityContexts());

	}

	@Bean(name = "Api3")
	public Docket api3() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).useDefaultResponseMessages(false)
				.groupName("收集管理").enable(true).select().apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.regex("^(?!auth).*$"))
				.apis(RequestHandlerSelectors.basePackage("com.daxt.controller.filemanager")).paths(PathSelectors.any())
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
				.build().securitySchemes(securitySchemes()).securityContexts(securityContexts());

	}

	@Bean(name = "Api4")
	public Docket api4() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).useDefaultResponseMessages(false)
				.groupName("全宗管理").enable(true).select().apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.regex("^(?!auth).*$"))
				.apis(RequestHandlerSelectors.basePackage("com.daxt.controller.sysarchives")).paths(PathSelectors.any())
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
				.build().securitySchemes(securitySchemes()).securityContexts(securityContexts());
	}

	@Bean(name = "Api5")
	public Docket api5() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).useDefaultResponseMessages(false)
				.groupName("admin").enable(true).select().apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.regex("^(?!auth).*$"))
				.apis(RequestHandlerSelectors.basePackage("com.daxt.controller.admin")).paths(PathSelectors.any())
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
				.build().securitySchemes(securitySchemes()).securityContexts(securityContexts());
	}

	@Bean(name = "Api6")
	public Docket api6() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).useDefaultResponseMessages(false)
				.groupName("信息管理").enable(true).select().apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.regex("^(?!auth).*$"))
				.apis(RequestHandlerSelectors.basePackage("com.daxt.controller.infomanager")).paths(PathSelectors.any())
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
				.build().securitySchemes(securitySchemes()).securityContexts(securityContexts());
	}

	@Bean(name = "Api7")
	public Docket api7() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).useDefaultResponseMessages(false)
				.groupName("档案利用").enable(true).select().apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.regex("^(?!auth).*$"))
				.apis(RequestHandlerSelectors.basePackage("com.daxt.controller.fileuse")).paths(PathSelectors.any())
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
				.build().securitySchemes(securitySchemes()).securityContexts(securityContexts());
	}

	@Bean(name = "Api8")
	public Docket api8() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).useDefaultResponseMessages(false)
				.groupName("web利用").enable(true).select().apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.regex("^(?!auth).*$"))
				.apis(RequestHandlerSelectors.basePackage("com.daxt.controller.webuse")).paths(PathSelectors.any())
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
				.build().securitySchemes(securitySchemes()).securityContexts(securityContexts());
	}
	@Bean(name = "Api9")
	public Docket api9() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).useDefaultResponseMessages(false)
				.groupName("库房").enable(true).select().apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.regex("^(?!auth).*$"))
				.apis(RequestHandlerSelectors.basePackage("com.daxt.controller.warehouse")).paths(PathSelectors.any())
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
				.build().securitySchemes(securitySchemes()).securityContexts(securityContexts());
	}
	
	@Bean(name = "Api10")
	public Docket api10() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).useDefaultResponseMessages(false)
				.groupName("编研").enable(true).select().apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.regex("^(?!auth).*$"))
				.apis(RequestHandlerSelectors.basePackage("com.daxt.controller.notice")).paths(PathSelectors.any())
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
				.build().securitySchemes(securitySchemes()).securityContexts(securityContexts());
	}
	@Bean(name = "Api11")
	public Docket api11() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).useDefaultResponseMessages(false)
				.groupName("模板管理").enable(true).select().apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.regex("^(?!auth).*$"))
				.apis(RequestHandlerSelectors.basePackage("com.daxt.controller.template")).paths(PathSelectors.any())
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
				.build().securitySchemes(securitySchemes()).securityContexts(securityContexts());
	}
	@Bean(name = "Api12")
	public Docket api12() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).useDefaultResponseMessages(false)
				.groupName("API管理").enable(true).select().apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.regex("^(?!auth).*$"))
				.apis(RequestHandlerSelectors.basePackage("com.daxt.controller.api")).paths(PathSelectors.any())
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
				.build().securitySchemes(securitySchemes()).securityContexts(securityContexts());
	}
	@Bean(name = "Api13")
	public Docket api13() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).useDefaultResponseMessages(false)
				.groupName("归档接口使用").enable(true).select().apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.regex("^(?!auth).*$"))
				.apis(RequestHandlerSelectors.basePackage("com.daxt.controller.accept")).paths(PathSelectors.any())
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
				.build().securitySchemes(securitySchemes()).securityContexts(securityContexts());
	}

	

	private List<SecurityScheme> securitySchemes() {
		SecurityScheme ss = new ApiKey("Authorization", "Authorization", "header");
		return Lists.newArrayList(ss);
	}

	@SuppressWarnings("deprecation")
	private List<SecurityContext> securityContexts() {
		return Lists.newArrayList(SecurityContext.builder().securityReferences(defaultAuth())
				.forPaths(PathSelectors.regex("^(?!auth).*$")).build());
	}

	List<SecurityReference> defaultAuth() {
		AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
		AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
		authorizationScopes[0] = authorizationScope;
		return Lists.newArrayList(new SecurityReference("Authorization", authorizationScopes));
	}

	// -------------

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
				.apis(RequestHandlerSelectors.basePackage("com.daxt.controller"))
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
				.build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("API") // System.getProperty("user.dir")
				.description(active + "文档说明").version("2.0").build();
	}
}
